﻿Imports System.Data.SqlClient

Public Class adm_merchantbaseinfo
    Inherits System.Web.UI.Page
    Private sSQL As String
    Private objrd As SqlDataReader = Nothing
    Private objfind As New GetData
    Private ps As New PreparedStatement(sSQL)

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        If (Session("admin_ID") <> Nothing And Session("admin_ID") <> "") Then
            If (Request("uid") <> Nothing And Request("uid") <> "") Then
                Dim companyName, country, state, city, address, companyMode, productField, companyMajor, productSell,
                    productBuy, linkMan, phone, cellphone As String
                Dim gender As Boolean
                Dim memberId, strErr As String
                Dim comId As String = ""
                Dim companyId As String = ""
                strErr = ""
                memberId = Request("uid")

                If (Request("submit") <> Nothing And Request("submit") <> "") Then

                    companyName = Request("c_companyName")
                    country = Request("country")
                    state = Request("state")
                    city = Request("city")
                    address = Request("c_address")
                    companyMode = Request("company_mode")
                    productField = Request("c_productField")
                    companyMajor = Request("company_major")
                    productSell = Request("c_productSell")
                    productBuy = Request("c_productBuy")
                    linkMan = Request("c_linkMan")
                    phone = Request("c_phone")
                    cellphone = Request("c_cellphone")
                    If (Request("gender").ToString = "1") Then
                        gender = True
                    Else
                        gender = False
                    End If

                    strErr = ""
                    ps.strSql = "select mm.* from member m,merchant mm where mm.company_id=m.company_id and m.GAPortal_ID=@memberId"
                    ps.addParameter("memberId", memberId)
                    Dim conn As SqlConnection = objfind.getConn()
                    objrd = objfind.dr(ps, strErr, conn, objrd)
                    If (strErr = "" And objrd.Read()) Then
                        ps.strSql = "update merchant set company_mode= @companyMode,company_major=@companyMajor where company_id=@companyId"
                        ps.addParameter("companyMode", companyMode)
                        ps.addParameter("companyMajor", companyMajor)
                        ps.addParameter("companyId", objrd.Item("company_id"))
                        objfind.es(ps, strErr)
                    Else
                        ps.strSql = "insert into merchant(company_id,company_mode,company_major,owner_id,active)" & _
                                    "values (@companyId,@companyMode,@companyMajor,@ownerId,@active)"
                        comId = System.Guid.NewGuid().ToString()
                        ps.addParameter("companyId", comId)
                        ps.addParameter("companyMode", companyMode)
                        ps.addParameter("companyMajor", companyMajor)
                        ps.addParameter("ownerId", memberId)
                        ps.addParameter("active", "1")
                        objfind.es(ps, strErr)
                    End If

                    ps.strSql = "select mi.* from member m,merchant_info mi where m.company_id=mi.company_id and mi.lang_flag='en' and m.GAPortal_ID=@memberId"
                    ps.parameters.Clear()
                    ps.addParameter("memberId", memberId)
                    objrd = objfind.dr(ps, strErr, conn, objrd)
                    If (strErr = "" And objrd.Read()) Then
                        ps.strSql = "update merchant_info set company_name = @companyName, country=@country ,state=@state,city=@city,address=@address,product_field=@productField ," & _
                            "product_sell=@productSell,product_buy=@productBuy,linkman=@linkMan,gender=@gender,phone=@phone,cellphone=@cellphone where id=@id"
                        ps.parameters.Clear()
                        ps.addParameter("companyName", companyName)
                        ps.addParameter("country", country)
                        ps.addParameter("state", state)
                        ps.addParameter("city", city)
                        ps.addParameter("address", address)
                        ps.addParameter("productField", productField)
                        ps.addParameter("productSell", productSell)
                        ps.addParameter("productBuy", productBuy)
                        ps.addParameter("linkman", linkMan)
                        ps.addParameter("gender", gender)
                        ps.addParameter("phone", phone)
                        ps.addParameter("cellphone", cellphone)
                        ps.addParameter("id", objrd.Item("id"))
                        objfind.es(ps, strErr)

                    Else
                        If (comId <> "" And comId <> Nothing) Then
                            companyId = comId
                        Else
                            ps.strSql = "select * from merchant where owner_id =@ownerId"
                            ps.addParameter("ownerId", memberId)
                            objrd = objfind.dr(ps, strErr, conn, objrd)
                            If (objrd.Read()) Then
                                companyId = objrd.Item("company_id")
                            End If
                        End If
                        ps.strSql = "insert into merchant_info (id,company_id,company_name,country,state,city,address,product_field,product_sell,product_buy,linkman,gender,phone,cellphone,lang_flag) " & _
                                    "values(@id,@companyId, @companyName, @country ,@state,@city,@address,@productField，@productSell,@productBuy,@linkMan,@gender,@phone,@cellphone,'en'））"

                        ps.addParameter("companyId", companyId)
                        ps.addParameter("companyName", companyName)
                        ps.addParameter("country", country)
                        ps.addParameter("state", state)
                        ps.addParameter("city", city)
                        ps.addParameter("address", address)
                        ps.addParameter("productField", productField)
                        ps.addParameter("productSell", productSell)
                        ps.addParameter("productBuy", productBuy)
                        ps.addParameter("linkman", linkMan)
                        ps.addParameter("gender", gender)
                        ps.addParameter("phone", phone)
                        ps.addParameter("cellphone", cellphone)
                        ps.addParameter("id", System.Guid.NewGuid().ToString())
                        objfind.es(ps, strErr)
                    End If

                    objfind.closeConn(conn)
                Else
                    Dim conn As SqlConnection = objfind.getConn()
                    ps.strSql = "select * from category where lang_flag='en'"
                    ps.parameters.Clear()
                    objrd = objfind.dr(ps, strErr, conn, objrd)
                    While (objrd.Read())
                        c_productField.Items.Add(New ListItem(
                        objrd("Category_Name").ToString(), objrd("Category_ID").ToString()))
                    End While
                    sSQL = "select mi.*,mm.company_mode,mm.company_major from member m,merchant mm,merchant_info mi where m.company_id=mi.company_id and mm.company_id=m.company_id and mi.lang_flag='en' and m.GAPortal_ID=@memberId"
                    ps.strSql = sSQL
                    ps.parameters.Clear()
                    ps.addParameter("memberId", memberId)
                    objrd = objfind.dr(ps, strErr, conn, objrd)
                    If (strErr = "" And objrd.Read()) Then
                        c_companyName.Text = objrd("company_name").ToString
                        country_code.Text = objrd("country").ToString
                        state_code.Text = objrd("state").ToString
                        city_code.Text = objrd("city").ToString
                        c_address.Text = objrd("address").ToString
                        If (objrd("company_mode").ToString.Trim() = "1") Then
                            Me.sctype.Checked = True
                        ElseIf (objrd("company_mode").ToString.Trim() = "2") Then
                            Me.mytype.Checked = True
                        ElseIf (objrd("company_mode").ToString.Trim() = "3") Then
                            Me.fwtype.Checked = True
                        End If

                        If (objrd("company_major").ToString.Trim() = "1") Then
                            Me.xsaspect.Checked = True
                        ElseIf (objrd("company_major").ToString.Trim() = "2") Then
                            Me.cgaspect.Checked = True
                        ElseIf (objrd("company_major").ToString.Trim() = "3") Then
                            Me.bothaspect.Checked = True
                        End If

                        c_productSell.Text = objrd("product_sell").ToString
                        c_productBuy.Text = objrd("product_buy").ToString
                        c_linkMan.Text = objrd("linkman").ToString
                        c_phone.Text = objrd("phone").ToString
                        c_cellphone.Text = objrd("cellphone").ToString
                        If (objrd("gender").ToString.Trim().ToLower() = "false") Then
                            Me.genderMan.Checked = True
                        ElseIf (objrd("gender").ToString.Trim().ToLower() = "true") Then
                            Me.genderWoman.Checked = True
                        End If
                        productField = objrd("product_field").ToString

                        'ps.strSql = "select * from category where lang_flag='en'"
                        'ps.parameters.Clear()
                        'objrd = objfind.dr(ps, strErr, conn, objrd)
                        'While (objrd.Read())
                        '    c_productField.Items.Add(New ListItem(
                        '    objrd("Category_Name").ToString(), objrd("Category_ID").ToString()))
                        'End While
                        c_productField.SelectedValue = productField
                    End If
                    
                    objfind.closeConn(conn)
                End If

            Else
                Response.Redirect("../memberMngt_default.aspx")
            End If
        Else
            Response.Redirect("../adm_login.aspx")
        End If

    End Sub

End Class